package com.tiankong.coupon.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiankong.coupon.dto.SkuCoupon;
import com.tiankong.coupon.dto.SpuCouponInfo;
import com.tiankong.coupon.entity.SkuFullReduction;
import com.tiankong.coupon.entity.SkuLadder;
import com.tiankong.coupon.entity.SpuBounds;
import com.tiankong.coupon.mapper.SpuBoundsMapper;
import com.tiankong.coupon.service.SkuFullReductionService;
import com.tiankong.coupon.service.SkuLadderService;
import com.tiankong.coupon.service.SpuBoundsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品spu积分设置 服务实现类
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@Service
public class SpuBoundsServiceImpl extends ServiceImpl<SpuBoundsMapper, SpuBounds> implements SpuBoundsService {

    @Autowired
    private SkuFullReductionService fullReductionService;
    @Autowired
    private SkuLadderService skuLadderService;
    @Override
    public void saveCoupon(SpuCouponInfo spuCouponInfo) {

        //spu 积分设置
        SpuBounds bound = spuCouponInfo.getBound();
        save(bound);

        //sku 优惠信息
        List<SkuCoupon> skuCoupons = spuCouponInfo.getSkuCoupon();

        //sku满减信息
        List<SkuFullReduction> fullReductions = new ArrayList<>();
        //sku 阶梯价格
        List<SkuLadder> skuLadders = new ArrayList<>();


        skuCoupons.forEach(sc->{
            if (sc.getFullPrice() != null && NumberUtil.isGreater(sc.getFullPrice(), BigDecimal.ZERO)){
                fullReductions.add(new SkuFullReduction().setFullPrice(sc.getFullPrice())
                        .setReducePrice(sc.getReducePrice()).setSkuId(sc.getSkuId()).setAddOther(sc.getPriceStatus()));
            }
            if (sc.getFullCount() != null && sc.getFullCount() > 0){
                skuLadders.add(new SkuLadder().setSkuId(sc.getSkuId()).setFullCount(sc.getFullCount())
                        .setAddOther(sc.getCountStatus()).setDiscount(sc.getDiscount()));
            }
        });

        if (!fullReductions.isEmpty()){
            fullReductionService.saveBatch(fullReductions);
        }
        if (!skuLadders.isEmpty()){
            skuLadderService.saveBatch(skuLadders);
        }
    }
}
